//
//  DFIvar.m
//  微商社区
//
//  Created by nahuo on 15/1/29.
//  Copyright (c) 2015年 nahuo. All rights reserved.
//

#import "DFIvar.h"
#import "DFType.h"

@implementation DFIvar


/**
 *  初始化
 *
 *  @param ivar      成员变量
 *  @param srcObject 哪个对象的成员变量
 *
 *  @return 初始化好的对象
 */
- (instancetype)initWithIvar:(Ivar)ivar
{
    if (self = [super init]) {
        self.ivar = ivar;
    }
    return self;
}

/**
 *  设置成员变量
 */
- (void)setIvar:(Ivar)ivar
{
    _ivar = ivar;

    // 1.成员变量名
    _name = [NSString stringWithUTF8String:ivar_getName(ivar)];
    
    // 2.属性名
    if ([_name hasPrefix:@"_"]) {
        _propertyName = [_name stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:@""];
    } else {
        _propertyName = _name;
    }
    
    // 3.成员变量的类型符
    NSString *code = [NSString stringWithUTF8String:ivar_getTypeEncoding(ivar)];
    if ([code hasPrefix:@"@"]) {
        // 去掉@"和"，截取中间的类型名称
        code = [code substringFromIndex:2];
        code = [code substringToIndex:code.length - 1];
        _type = NSClassFromString(code);
    }
    
}


-(void)setValue:(id)value forObject:(id)object {
    [object setValue:value forKey:_propertyName];
}

-(id)valueForObject:(id)object {
    return [object valueForKey:_propertyName];
}

@end
